package com.youlin.controller;

import com.youlin.dto.BaseDataResDto;
import com.youlin.dto.BaseResDto;
import com.youlin.dto.Status;
import com.youlin.service.MyTestService;
import com.youlin.untils.UnIntercept;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.time.Duration;
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/myTest")
public class MyTestController {

    @Autowired
    private MyTestService myTestService;
    @Autowired
    private RedissonClient redissonClient;

    @UnIntercept
    @GetMapping("/guest/testRedissonLock")
    public BaseDataResDto<LocalTime> testRedissonLock () {
        RLock redissonLock = redissonClient.getLock("testRedissonLock");
        try {
            System.out.printf("\033[33m 准备入坑 ===>> %s \033[0m\n", LocalTime.now());

            redissonLock.lock(10000, TimeUnit.MILLISECONDS);
            TimeUnit.SECONDS.sleep(15);

            System.out.printf("\033[32m 我已出坑，感觉良好 ===>> %s \033[0m\n", LocalTime.now());
            return new BaseDataResDto<LocalTime>(Status.SUCCESS).setData(LocalTime.now());
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseDataResDto<>(Status.ERROR);
        }
    }

    @UnIntercept
    @GetMapping("/guest/saveJdbc1")
    public BaseResDto saveJdbc1 () {
        myTestService.saveJdbc1();
        return new BaseResDto(Status.SUCCESS);
    }

    @UnIntercept
    @GetMapping("/guest/saveJdbc3")
    public BaseResDto saveJdbc3 () {
        myTestService.saveJdbc3_3();
        return new BaseResDto(Status.SUCCESS);
    }

    @UnIntercept
    @GetMapping("/guest/testTransactionIsolationLevel")
    public BaseResDto testTransactionIsolationLevel () {
        myTestService.testTransactionIsolationLevel();
        return new BaseResDto(Status.SUCCESS);
    }

    @UnIntercept
    @GetMapping("/guest/sendRabbitMsg")
    public BaseResDto sendRabbitMsg () {
        myTestService.sendRabbitMsg();
        return new BaseResDto(Status.SUCCESS);
    }

    @UnIntercept
    @GetMapping(value = "/guest/testFlux1", produces = {MediaType.APPLICATION_STREAM_JSON_VALUE})
    public Flux<Integer> testFlux1 () throws Exception {
        return Flux.just(1, 2, 3, 4, 5, 6).delayElements(Duration.ofSeconds(1));
    }

    @UnIntercept
    @GetMapping("/guest/download")
    public void download (HttpServletResponse response) throws Exception {
        response.setContentType("application/binary;charset=gbk");
//        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("123.txt", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        File file = new File("MyClass.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[1024];
        while (fileInputStream.read(bytes) != -1) {
            outputStream.write(bytes);
        }
        outputStream.close();
        fileInputStream.close();
    }

}
